<?php
//   $image=['.png','.img','.gif','.js'];
//   if(!empty($_POST)){
//       $name=$_FILES['image']['name'];
//       $key=strrchr($name,'.');
//       if(in_array($key,$image)){
//         if($_FILES['image']['error']==0){
//             $name=uniqid('pic').rand(0,100).$_FILES['image']['name'];
//             move_uploaded_file($_FILES['image']['tmp_name'],'./upload/'.$name);
//             echo "上传成功";
//         }else{
//            echo "上传失败";
//         }
//       }else{
//           echo "文件格式失败";
//       }
//   }

//   方法2

// $image=['image/png','image/img','image/gif',];
// if(!empty($_POST)){
//     $name=$_FILES['image']['type'];
//     $key=strrchr($name,'.');
//     if(in_array($key,$image)){
//       if($_FILES['image']['error']==0){
//           $name=uniqid('pic').rand(0,100).$_FILES['image']['name'];
//           move_uploaded_file($_FILES['image']['tmp_name'],'./upload/'.$name);
//           echo "上传成功";
//       }else{
//          echo "上传失败";
//       }
//     }else{
//         echo "文件格式失败";
//     }
// }

// 方法3
if(!empty($_POST)){
$image=['image/png','image/img','image/gif'];
$info=finfo_open(FILEINFO_MIME_TYPE);
$min=finfo_file($info,$_FILES['image']['tmp_name']);
if(in_array($min,$image)){
    if($_FILES['image']['error']==0){
        $name=uniqid('pic').rand(0,100).$_FILES['image']['name'];
        move_uploaded_file($_FILES['image']['tmp_name'],'./upload/'.$name);
        echo "上传成功";
    }else{
       echo "上传失败";
    }
  }else{
      echo "文件格式失败";
  }
}
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="./文件格式验证.php" method="post"  enctype="multipart/form-data">
     <input type="file" name="image">
     <input type="submit" value="上传" name="sub">
    </form>
</body>
</html>